/**
 *  JSimProd - Java simulation code for production processes
 *  Copyleft Jorge Mira Yagüe <jorge.mira.yague@gmail.com>
 *  Universidad de Alicante 2009
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package gui.tools.models;

import java.util.List;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import lib.model.BufferedStation;
import lib.model.Station;

/**
 * This class is used as a model to show stations
 * It shows the most relevant properties of the station as the name, the queue type,
 * and if it's buffered or not
 * It allows to add and remove stations from the model, updating the view of the table
 * @see Station
 * @see JTable
 * @see TableModel
 * @see DefaultTableModel
 * @author Jorge Mira Yagüe
 */
public class TMStation extends DefaultTableModel {

    private List<Station> stations;

    /**
     * Class builder
     * @param stations The station list to show in the table
     */
    public TMStation(List<Station> stations) {
        super();
        
        columnIdentifiers.add("Name");
        columnIdentifiers.add("Queue type");
        columnIdentifiers.add("Buffered");
        this.setStations(stations);
    }

    public List<Station> getStations() {
        return stations;
    }

    public void setStations(List<Station> stations) {
        this.stations = stations;
        refresh();
    }

    public Station getStation(int index) {
        return stations.get(index);
    }

    public Station getItemCategory(int index) {
        return stations.get(index);
    }

    public void setStation(int index, Station station) {
        stations.set(index, station);
        refresh();
    }

    public void addStation(Station station) {
        stations.add(station);
        refresh();
    }

    public void removeStation(int index) {
        stations.remove(index);
        refresh();
    }

    private void refresh() {
        dataVector = new Vector();
        Vector<String> v;
        for (Station s : stations) {
            v = new Vector();
            v.add(s.getName());
            v.add(s.getQueueType().toString());
            v.add(String.valueOf(s instanceof BufferedStation));
            dataVector.add(v);
        }
    }

    /**
     * This function makes the cells not editables by the user
     * @param x
     * @param y
     * @return always false
     */
    @Override
    public boolean isCellEditable(int x, int y) {
        return false;
    }
}
